package com.lz.controller;

import com.lz.pojo.SysPermission;
import com.lz.pojo.SysUser;
import com.lz.security.pojo.LoginUser;
import com.lz.security.utils.SecurityUtil;
import com.lz.service.SysPermissionService;
import com.lz.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.List;

@RestController
@RequestMapping("/permission")
public class SysPermissionController {

    @Autowired
    private SysPermissionService sysPermissionService;

    /**
     * 分页查询所有数据
     */
    @GetMapping("/page")
    @PreAuthorize("hasAnyAuthority('sys:permission:query')")
    public Result page(@RequestParam("pageNumber") Integer pageNumber,
                       @RequestParam("pageSize") Integer pageSize,
                       SysPermission sysPermission){
        return Result.success(sysPermissionService.pageVo(pageNumber,pageSize,sysPermission));
    }

    /**
     * 查询树形结构
     */
    @PreAuthorize("hasAnyAuthority('sys:permission:query')")
    @GetMapping("/tree")
    public Result tree(){
        return Result.success(sysPermissionService.tree());
    }

    /**
     * 查询当前用户拥有的权限列表
     */
    @GetMapping("/nav")
    public Result nav(){
        return Result.success(sysPermissionService.listByUserId(SecurityUtil.getUserId()));
    }

    /**
     * 查询所有数据
     */
    @GetMapping("/list")
    @PreAuthorize("hasAnyAuthority('sys:permission:query')")
    public Result list(){
        return Result.success(sysPermissionService.list());
    }

    /**
     * 通过主键查询单条数据
     */
    @GetMapping("/get/{id}")
    @PreAuthorize("hasAnyAuthority('sys:permission:query')")
    public Result get(@PathVariable("id") Integer id){
        return Result.success(sysPermissionService.getById(id));
    }

    /**
     * 新增数据
     */
    @PostMapping("/save")
    @PreAuthorize("hasAnyAuthority('sys:permission:save')")
    public Result save(@RequestBody SysPermission sysPermission){
        return Result.success(sysPermissionService.save(sysPermission));
    }

    /**
     * 编辑数据
     */
    @PutMapping("/edit")
    @PreAuthorize("hasAnyAuthority('sys:permission:update')")
    public Result edit(@RequestBody SysPermission sysPermission){
        return Result.success(sysPermissionService.updateById(sysPermission));
    }

    /**
     * 单条删除数据
     */
    @DeleteMapping("/remove/{id}")
    @PreAuthorize("hasAnyAuthority('sys:permission:del')")
    public Result remove(@PathVariable("id") Integer id){
        return Result.success(sysPermissionService.removeById(id));
    }

    /**
     * 批量删除数据
     */
    @DeleteMapping("/remove")
    @PreAuthorize("hasAnyAuthority('sys:permission:del')")
    public Result removeBatch(@RequestParam("idList") List<Integer> idList){
        return Result.success(sysPermissionService.removeBatchByIds(idList));
    }

}
